package org.jeecg.modules.wlhy.customer.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.wlhy.customer.dto.HyCustomerAdminDTO;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAdmin;
import org.jeecg.modules.wlhy.customer.mapper.HyCustomerAdminMapper;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAdminService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description: 业务管理员与货主的关系
 */
@Service
public class HyCustomerAdminServiceImpl extends ServiceImpl<HyCustomerAdminMapper, HyCustomerAdmin> implements IHyCustomerAdminService {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Autowired
    private IHyCustomerService hyCustomerService;

    @Override
    public Result getPage(Page<HyCustomerAdmin> page, HyCustomerAdminDTO customerAdminDTO) {

        QueryWrapper<HyCustomerAdminDTO> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(customerAdminDTO.getRealName())) {
            queryWrapper.like("su.real_name", customerAdminDTO.getRealName());
        }
        if (StrUtil.isNotEmpty(customerAdminDTO.getCustomerName())) {
            queryWrapper.like("hc.customer_name", customerAdminDTO.getCustomerName());
        }

        IPage<HyCustomerAdminDTO> pageList = this.baseMapper.getPage(page, queryWrapper);
        return Result.OK(pageList);
    }

    @Override
    public Result add(HyCustomerAdminDTO customerAdminDTO) {

        SysUser sysUser = this.sysUserService.getOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getId, customerAdminDTO.getUserId()));
        if (null == sysUser) {
            return Result.error("业务管理员账号不存在");
        }

        HyCustomer hyCustomer = this.hyCustomerService.getOne(new LambdaQueryWrapper<HyCustomer>()
                .eq(HyCustomer::getId, customerAdminDTO.getCustomerId()));
        if (null == hyCustomer) {
            return Result.error("货主账号不存在");
        }

        SysUserRole sysUserRole = this.sysUserRoleService.getOne(new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getUserId, customerAdminDTO.getUserId()));

        if (0 < this.count(new LambdaQueryWrapper<HyCustomerAdmin>()
                .eq(HyCustomerAdmin::getUserId, customerAdminDTO.getUserId())
                .eq(HyCustomerAdmin::getCustomerId, customerAdminDTO.getCustomerId()))) {
            return Result.error("该业务员已关联该货主");
        }

        HyCustomerAdmin hyCustomerAdmin = HyCustomerAdmin.builder()
                .userId(customerAdminDTO.getUserId())
                .customerId(customerAdminDTO.getCustomerId())
                .roleId(sysUserRole.getRoleId())
                .build();
        this.save(hyCustomerAdmin);

        return Result.OK();
    }


}
